﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace IRongs.Wbt.Processor2
{
    public enum Method { GET, POST, PUT, DELETE };

    public static class WebRequestHelper
    {
        public static string WebRequest(string url, string postData, Method method)
        {
            HttpWebRequest webRequest = null;
            webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;

            return WebRequest(webRequest, postData, method);
        }

        public static string WebRequest(HttpWebRequest webRequest, string postData, Method method)
        {
            string responseData = "";

            webRequest.Method = method.ToString();
            webRequest.ServicePoint.Expect100Continue = false;

            if (method == Method.POST)
            {
                webRequest.ContentType = "application/x-www-form-urlencoded";
                using (StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream()))
                {
                    requestWriter.Write(postData);
                }
            }


            using (Stream stream = webRequest.GetResponse().GetResponseStream())
            {
                using (StreamReader responseReader = new StreamReader(stream))
                {
                    responseData = responseReader.ReadToEnd();
                }
            }

            return responseData;
        }
    }
}
